package com.vimtec.remote.client;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.net.Socket;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

import com.vimtec.remote.IRobot;

public class RemoteClient extends JFrame
{

	private String host = null;
	
	private int imageServerPort = 880;
	
	private int eventServerPort = 881;
	
	private IRobot iRobot = null;
	
	private JLabel imageDisplay = null;
	public RemoteClient(String server) throws IllegalArgumentException, IOException
	{
			host = server;
			initialize();
	}
	
	public RemoteClient(String server, int imPort, int evPort) throws IllegalArgumentException, IOException
	{
		
		host = server;
		imageServerPort = imPort;
		eventServerPort = evPort;
		initialize();
	}
	
	public static void main(String args[]) throws IllegalArgumentException, IOException
	{
		if(args.length==3)
		{
			String host = args[0];
			int imPort = Integer.parseInt(args[1]);
			int evPort = Integer.parseInt(args[2]);
			RemoteClient client = new RemoteClient(host,imPort,evPort);
		}
	}
	public void  initialize() throws IllegalArgumentException, IOException
	{
		setTitle(host + " : " + imageServerPort);
		iRobot = (IRobot)Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class [] {IRobot.class}, new RemoteIRobotHandler(host,eventServerPort));
		imageDisplay = new JLabel();
		getContentPane().add(new JScrollPane(imageDisplay));
		
		Thread imThread = new Thread(new ImageUpdater());
		imThread.start();
		DesktopEventsHandler mousehandler = new DesktopEventsHandler();
		imageDisplay.addMouseListener(mousehandler);
		imageDisplay.addMouseMotionListener(mousehandler);
		imageDisplay.addMouseWheelListener(mousehandler);


		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400,400);
		setVisible(true);
		
	}
	
	private class DesktopEventsHandler implements MouseListener,MouseMotionListener,MouseWheelListener
	{

		@Override
		public void mouseClicked(MouseEvent e)
		{
			iRobot.mouseClick(e.getButton());
		}

		@Override
		public void mouseEntered(MouseEvent e)
		{
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e)
		{
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e)
		{
			iRobot.mousePress(e.getButton());
		}

		@Override
		public void mouseReleased(MouseEvent e)
		{
			iRobot.mouseRelease(e.getButton());
		}

		@Override
		public void mouseDragged(MouseEvent e)
		{
			mouseMoved(e);
		}

		@Override
		public void mouseMoved(MouseEvent e)
		{
			iRobot.mouseMove(e.getX(), e.getY());
			
		}

		@Override
		public void mouseWheelMoved(MouseWheelEvent e)
		{
			iRobot.mouseWheel(e.getWheelRotation());
		}
		
	}
	
	private class ImageUpdater implements Runnable
	{
		Socket sock = null;
		InputStream in = null;
		
		public ImageUpdater() throws IOException
		{
			sock = new Socket();
			sock.connect(new InetSocketAddress(host,imageServerPort));
			in = sock.getInputStream();
		}
		public void run()
		{
			while(true)
			{
				try
				{
					byte[] bytes = new byte[1024*1024];
					int count = 0;
					do
					{
						count+= in.read(bytes,count,bytes.length-count);
					}
					while(!(count>4&&bytes[count-2]==(byte)-1 && bytes[count-1]==(byte)-39));
					BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
					imageDisplay.setIcon(new ImageIcon(image));
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
